package com.yuji.aacar.utils;

import android.text.TextUtils;

import com.yuji.aacar.core.cores.utils.LogUtil;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Pattern;

import static java.lang.Integer.parseInt;

/**
 * <li>Package:com.yuji.aacar.utils</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/3/10</li>
 * <li>Description:   </li>
 */

public class TimeUtils {

    public static int DELAY_TIME = 30;


    /**
     * 年月日
     *
     * @return
     */
    public static String getSystemWashDate(int whichDay) {

        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, whichDay);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);

        return dateString;
    }


    /**
     * 年月日  十分   2个小时后时间
     *
     * @return
     */
    public static String[] getTwoHoursLaterTime(String dayTime) {
        Date date = null;
        String dateNow = null;
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try {
            date = format1.parse(dayTime);
            long twoHourTime = date.getTime() + 2 * 60 * 60 * 1000;
            dateNow = format1.format(new Date(twoHourTime));
            LogUtil.e("转化后的 2个小时以后的时间", dateNow);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String[] times = dateNow.split(" ")[1].split(":");

        return times;
    }


    /**
     * 系统当前  4：20
     *
     * @return
     */
    public static String getSystemWashTime() {
        Calendar calendar = Calendar.getInstance();
        //小时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        //分钟
        int minute = calendar.get(Calendar.MINUTE);

        return hour + ":" + minute;
    }


    /**
     * 2:30 半个小时以后的时间
     *
     * @return
     */
    public static String getWashConvertDayTime(String hour, String minute) {

        int mHour = Integer.parseInt(hour);
        int mMin = parseInt(minute);

        int lastMin = 0;

        if (mMin >= 30) {
            mHour = mHour + 1;
            lastMin = mMin + DELAY_TIME - 60;
        } else {
            lastMin = mMin + DELAY_TIME;
        }

        if (lastMin == 0) {
            lastMin = 0;
        } else if (lastMin >= 0 && lastMin <= 10) {
            lastMin = 10;
        } else if (lastMin > 10 && lastMin <= 20) {
            lastMin = 20;
        } else if (lastMin > 20 && lastMin <= 30) {
            lastMin = 30;
        } else if (lastMin > 30 && lastMin <= 40) {
            lastMin = 40;
        } else if (lastMin > 40 && lastMin <= 50) {
            lastMin = 50;
        } else if (lastMin > 50 && lastMin <= 59) {
            mHour = mHour + 1;
            lastMin = 0;
        }
        return mHour + ":" + lastMin;
    }


    //    public static final String calHourAndTime(int hour, int time) {
    //        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //        long timeNow= System.currentTimeMillis();
    //        long timeLose=System.currentTimeMillis()+2*60*60*1000;
    //
    //        System.out.println(timeNow+"  "+timeNow);
    //
    //
    //        String dateNow=sdf.format(new Date(timeNow));
    //        String loseDate=sdf.format(new Date(timeLose));
    //
    //        System.out.println("申请全局access_token时间="+dateNow+"    全局access_token失效时间为="+loseDate);
    //
    //        return format.format(date);
    //    }


    /**
     * 获取当前时间
     *
     * @return
     */
    public static final String getCurrentTime() {
        long time = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        return format.format(date);
    }


    public static String formatDate(long date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date d = new Date(date);
        String str = sdf.format(d);
        String[] index = str.split("-");
        String data = index[0] + "年" + index[1] + "月";
        return data;
    }

    public static String formatDate(long date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d = new Date(date);
        return sdf.format(d);
    }


    public static String dateFormat(String date) {
        return date.replace("年", "-").replace("月", "-").replace("日", "");
    }

    //yyyy年MM月dd日改成mm-dd
    public static String dateFormatYearSub(String date) {
        return date.substring(5).replace("月", ".").replace("日", "");
    }

    public static String getCheckTime(String s, String pattern) {
        if ("null".equals(s) || TextUtils.isEmpty(s)) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(new Date(Long.parseLong(s)));
    }

    /*******
     * 2017-05 转成一个时间
     * @param s
     * @param pattern
     * @return
     */
    public static long getCheckDateForTime(String s, String pattern) {
        if ("null".equals(s) || TextUtils.isEmpty(s)) {
            return 0;
        }


        SimpleDateFormat sdf = new SimpleDateFormat(pattern);

        try {
            return sdf.parse(s).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 是否是数字
     *
     * @param str
     * @return
     */
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     *
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    /**
     * 将分为单位的转换为元 （除100）
     *
     * @param amount
     * @return
     * @throws Exception
     */
    public static String changeF2Y(String amount) {
        if (!amount.matches("\\-?[0-9]+")) {
            return "";
        }
        return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString();
    }


    //根据秒数转化为时分秒   00:00:00
    public static String getTime(int second) {
        if (second < 10) {
            return "00:0" + second;
        }
        if (second < 60) {
            return "00:" + second;
        }
        if (second < 3600) {
            int minute = second / 60;
            second = second - minute * 60;
            if (minute < 10) {
                if (second < 10) {
                    return "0" + minute + ":0" + second;
                }
                return "0" + minute + ":" + second;
            }
            if (second < 10) {
                return minute + ":0" + second;
            }
            return minute + ":" + second;
        }
        int hour = second / 3600;
        int minute = (second - hour * 3600) / 60;
        second = second - hour * 3600 - minute * 60;
        if (hour < 10) {
            if (minute < 10) {
                if (second < 10) {
                    return "0" + hour + ":0" + minute + ":0" + second;
                }
                return "0" + hour + ":0" + minute + ":" + second;
            }
            if (second < 10) {
                return "0" + hour + minute + ":0" + second;
            }
            return "0" + hour + minute + ":" + second;
        }
        if (minute < 10) {
            if (second < 10) {
                return hour + ":0" + minute + ":0" + second;
            }
            return hour + ":0" + minute + ":" + second;
        }
        if (second < 10) {
            return hour + minute + ":0" + second;
        }
        return hour + minute + ":" + second;
    }


    /*
    * 将时间戳转换为时间
    */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

}
